package com.baidu.shoppingmall.role.service.Impl;


import com.baidu.shoppingmall.role.constant.UserLoginInformation;
import com.baidu.shoppingmall.role.dao.UserDao;
import com.baidu.shoppingmall.role.domain.UserDTO;
import com.baidu.shoppingmall.role.domain.UserPO;
import com.baidu.shoppingmall.role.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserPO> implements UserService {

    @Autowired
    private UserDao userDao;

    private static  final Logger LOGGER= LoggerFactory.getLogger(UserServiceImpl.class);


    @Override
    public Boolean login(String name,String password) {
        try {
            QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = new HashMap<>();
            map.put("accountNo", name);
            map.put("password", password);
            queryWrapper.allEq(map);
            UserPO userPO = userDao.selectOne(queryWrapper);
            if (userPO==null){
                return UserLoginInformation.NO.getStatus();
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("error", e);
            return UserLoginInformation.NO.getStatus();
        }
        return UserLoginInformation.YES.getStatus();
    }

    @Override
    public String queryId(String name, String password) {
        UserPO userPO = null;
        try {
            QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = new HashMap<>();
            map.put("accountNo", name);
            map.put("password", password);
            queryWrapper.allEq(map);
            userPO = userDao.selectOne(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("error", e);
            return null;
        }
        return userPO.getId();
    }
}
